﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Text;
using CNVP.Config;

namespace CNVP.JShop.BLL
{
    /// <summary>
    /// 购物车类
    /// </summary>
    [Serializable]
    public class BLLShoppingCar
    {
        BLL.CNVP_Products bll_Products = new BLL.CNVP_Products();
        /// <summary>
        /// 从Cookies中读取要购买的商品
        /// </summary>
        /// <returns></returns>
        public IList<Model.Cust_ProductsDetail> GetDataFromCookies()
        {
            IList<Model.Cust_ProductsDetail> iList = new List<Model.Cust_ProductsDetail>();
            string ShoppingCart_ProductID = HttpContext.Current.Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProductID"];
            string ShoppingCart_Amount = HttpContext.Current.Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["Amount"];
            if (!string.IsNullOrEmpty(ShoppingCart_ProductID) && !string.IsNullOrEmpty(ShoppingCart_Amount))
            {
                string[] split = ShoppingCart_ProductID.Split(new Char[] { ',' });
                string[] splitAmount = ShoppingCart_Amount.Split(new Char[] { ',' });

                for (int i = 0, j = 0; i < split.Length && j < splitAmount.Length; i++, j++)
                {
                    int proID = int.Parse(split[i].ToString());//主产品ID
                    int proAmount = int.Parse(splitAmount[j].ToString());

                    string _proDetailID = string.Empty;
                    string _ProDetailNo = "";
                    string _StandardValueName = "";
                    decimal _SalePrice = 0;
                    string ShoppingCart_ProDetailID = HttpContext.Current.Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProDetailID"];
                    if (!string.IsNullOrEmpty(ShoppingCart_ProDetailID))
                    {
                        string[] arr = ShoppingCart_ProDetailID.Split(new Char[] { ',' });
                        if (!string.IsNullOrEmpty(arr[i]) && arr[i] != "0")
                        {
                            BLL.CNVP_ProductsDetail bll_proDetail = new BLL.CNVP_ProductsDetail();
                            Model.CNVP_ProductsDetail model_proDetail = new Model.CNVP_ProductsDetail();
                            model_proDetail = bll_proDetail.GetSingle_CNVP_ProductsDetailById(Convert.ToInt32(arr[i]));
                            if (model_proDetail != null)
                            {
                                _proDetailID = Convert.ToString(arr[i]);
                                _ProDetailNo = model_proDetail.ProDetailNo;
                                string _StandardValueIDs = model_proDetail.StandardValueIDs;
                                if (!string.IsNullOrEmpty(_StandardValueIDs))
                                {
                                    string StandardValueName = "（";
                                    string[] StandardValueIDsArray = _StandardValueIDs.Split(Convert.ToChar(","));
                                    for (int k = 0; k < StandardValueIDsArray.Length; k++)
                                    {
                                        var model_StandardValue = new BLL.CNVP_StandardValue().GetSingle_CNVP_StandardValueById(int.Parse(StandardValueIDsArray[k]));
                                        if (model_StandardValue != null)
                                        {
                                            StandardValueName += model_StandardValue.StandardValueName + "、";
                                        }
                                    }
                                    StandardValueName = StandardValueName.Substring(0, StandardValueName.Length - 1) + "）";
                                    _StandardValueName = StandardValueName;
                                }
                                _SalePrice = model_proDetail.SalePrice;
                            }
                        }
                        else
                        {
                            Model.CNVP_Products model_P = bll_Products.GetSingle_CNVP_ProductsById(proID);
                            if (model_P != null)
                            {
                                _ProDetailNo = model_P.ProductNo;
                                _SalePrice = model_P.TradePrice;
                            }
                        }
                    }
                    #region 产品购物车
                    Model.CNVP_Products model_Products = bll_Products.GetSingle_CNVP_ProductsById(proID);
                    if (model_Products != null)
                    {
                        Model.Cust_ProductsDetail uShoppingcartInfo = new Model.Cust_ProductsDetail();
                        //主商品ID
                        uShoppingcartInfo.ProID = proID;
                        //产品类别
                        uShoppingcartInfo.ProductClassID = model_Products.ProductClassID;
                        //详细产品ID
                        uShoppingcartInfo.Product_ID = Convert.ToInt32(string.IsNullOrEmpty(_proDetailID) ? "0" : _proDetailID);//详细产品ID
                        //商品名称
                        uShoppingcartInfo.Product_Name = model_Products.ProductName;
                        //商品编号
                        uShoppingcartInfo.ProductDetail_No = _ProDetailNo + "" + _StandardValueName;
                        //商品价格
                        uShoppingcartInfo.Product_Price = Convert.ToString(_SalePrice);
                        //图片   
                        uShoppingcartInfo.Product_Image = model_Products.ImgProLoad;
                        //商品数量
                        uShoppingcartInfo.Amount = proAmount;
                        iList.Add(uShoppingcartInfo);
                    }
                    else
                    {
                        Delete(proID.ToString(), _proDetailID, proAmount.ToString());//删除
                    }
                    #endregion
                }
            }
            return iList;

        }


        /// <summary>
        /// 清空购物车
        /// </summary>
        public void Clear()
        {
            //删除Cookies 
            HttpContext.Current.Response.Cookies["" + UIConfig.CookiesName + "ShoppingCart"].Expires = DateTime.Parse("1980-01-01");
        }

        /// <summary>
        /// 删除购物车中的商品
        /// </summary>       
        public void Delete(String ProductID, String ProDetailID, String Amount)
        {
            try
            {
                String sOldProductID = HttpContext.Current.Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProductID"];
                String sOldProductDetailID = HttpContext.Current.Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProDetailID"];
                String sOldAmount = HttpContext.Current.Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["Amount"];
                if (!string.IsNullOrEmpty(sOldProductID) && !string.IsNullOrEmpty(sOldProductDetailID) && !string.IsNullOrEmpty(sOldAmount))
                {
                    String sNewProID = null;
                    String sNewProDetailID = null;
                    String sNewAmount = null;

                    string[] split_ProductID = ProductID.Split(new Char[] { ',' });
                    string[] split_ProDetailID = ProDetailID.Split(new Char[] { ',' });
                    string[] split_Amount = Amount.Split(new Char[] { ',' });
                    //主id
                    string[] split_id = sOldProductID.Split(new Char[] { ',' });
                    //详细id
                    string[] split_prodetailid = sOldProductDetailID.Split(new Char[] { ',' });
                    //价格
                    string[] split_am = sOldAmount.Split(new Char[] { ',' });

                    ArrayList arr1 = new ArrayList();
                    ArrayList arr2 = new ArrayList();
                    ArrayList arr3 = new ArrayList();

                    #region 主ID
                    for (int n1 = 0; n1 < split_id.Length; n1++)
                    {
                        arr1.Add(split_id[n1]);
                    }
                    //删除传入的ID
                    for (int k = 0; k < split_ProductID.Length; k++)
                    {
                        arr1.Remove(split_ProductID[k]);
                    }
                    #endregion

                    #region 数量
                    for (int n2 = 0; n2 < split_am.Length; n2++)
                    {
                        arr2.Add(split_am[n2]);
                    }
                    //删除传入的数量
                    for (int k = 0; k < split_Amount.Length; k++)
                    {
                        arr2.Remove(split_Amount[k]);
                    }
                    #endregion

                    #region 详细ID
                    for (int n3 = 0; n3 < split_prodetailid.Length; n3++)
                    {
                        arr3.Add(split_prodetailid[n3]);
                    }
                    //删除传入的数量
                    for (int k = 0; k < split_ProDetailID.Length; k++)
                    {
                        arr3.Remove(split_ProDetailID[k]);
                    }
                    #endregion

                    //从新的数组中取得没有被删除的数据

                    for (int i1 = 0; i1 < arr1.Count; i1++)
                    {
                        sNewProID += arr1[i1].ToString() + ",";
                    }
                    for (int i2 = 0; i2 < arr2.Count; i2++)
                    {
                        sNewAmount += arr2[i2].ToString() + ",";
                    }
                    for (int i3 = 0; i3 < arr3.Count; i3++)
                    {
                        sNewProDetailID += arr3[i3].ToString() + ",";
                    }

                    //排除最后的","

                    if (sNewProID != null)
                    {
                        sNewProID = sNewProID.Substring(0, sNewProID.Length - 1);
                    }
                    else
                    {
                        sNewProID = "";
                    }
                    if (sNewAmount != null)
                    {
                        sNewAmount = sNewAmount.Substring(0, sNewAmount.Length - 1);
                    }
                    else
                    {
                        sNewAmount = "";
                    }

                    if (sNewProDetailID != null)
                    {
                        sNewProDetailID = sNewProDetailID.Substring(0, sNewProDetailID.Length - 1);
                    }
                    else
                    {
                        sNewProDetailID = "";
                    }

                    if (sNewProID != "" && sNewAmount != "")
                    {
                        WriteCookies(sNewProID, sNewProDetailID, sNewAmount);
                    }
                    else
                    {
                        this.Clear();
                    }
                }
            }
            catch { }

        }

        /// <summary>
        /// 将要购买的商品信息写入Cookies
        /// </summary>       
        public void WriteCookies(String ProductID, String ProDetailID, String Amount)
        {
            HttpContext.Current.Response.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProductID"] = ProductID;
            HttpContext.Current.Response.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProDetailID"] = ProDetailID;
            HttpContext.Current.Response.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["Amount"] = Amount;
            HttpContext.Current.Response.Cookies["" + UIConfig.CookiesName + "ShoppingCart"].Expires = DateTime.Now.AddDays(60);
        }
        /// <summary>
        /// 修改商品数量
        /// </summary>
        /// <param name="ProductID">主表ID</param>
        /// <param name="ProDetailID">详细表ID</param>
        /// <param name="Amount">商品数量</param>
        public void UpdateAmount(String ProductID, String ProDetailID, String Amount)
        {
            String sOldProductID = HttpContext.Current.Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProductID"];
            String sOldProductDetailID = HttpContext.Current.Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProDetailID"];
            String sOldAmount = HttpContext.Current.Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["Amount"];
            if (!string.IsNullOrEmpty(sOldProductID) && !string.IsNullOrEmpty(sOldProductDetailID) && !string.IsNullOrEmpty(sOldAmount))
            {
                String sNewProID = null;
                String sNewProDetailID = null;
                String sNewAmount = null;

                //主id
                string[] split_id = sOldProductID.Split(new Char[] { ',' });
                //详细id
                string[] split_prodetailid = sOldProductDetailID.Split(new Char[] { ',' });
                //价格
                string[] split_am = sOldAmount.Split(new Char[] { ',' });

                ArrayList arr1 = new ArrayList();
                ArrayList arr2 = new ArrayList();
                ArrayList arr3 = new ArrayList();

                for (int n1 = 0; n1 < split_id.Length; n1++)
                {
                    arr1.Add(split_id[n1]);
                    if (split_id[n1] == ProductID)
                    {
                        //注意对详细表ID的判断
                        if (split_prodetailid[n1] == ProDetailID)
                        {
                            arr2.Add(Amount);
                            arr3.Add(ProDetailID);
                        }
                        else
                        {
                            arr2.Add(split_am[n1]);
                            arr3.Add(split_prodetailid[n1]);
                        }
                    }
                    else
                    {
                        arr2.Add(split_am[n1]);
                        arr3.Add(split_prodetailid[n1]);
                    }
                }

                //从新的数组中取得没有被更新的数据
                for (int i1 = 0; i1 < arr1.Count; i1++)
                {
                    sNewProID += arr1[i1].ToString() + ",";
                }
                for (int i2 = 0; i2 < arr2.Count; i2++)
                {
                    sNewAmount += arr2[i2].ToString() + ",";
                }
                for (int i3 = 0; i3 < arr3.Count; i3++)
                {
                    sNewProDetailID += arr3[i3].ToString() + ",";
                }

                //排除最后的","

                if (sNewProID != null)
                {
                    sNewProID = sNewProID.Substring(0, sNewProID.Length - 1);
                }
                else
                {
                    sNewProID = "";
                }
                if (sNewAmount != null)
                {
                    sNewAmount = sNewAmount.Substring(0, sNewAmount.Length - 1);
                }
                else
                {
                    sNewAmount = "";
                }

                if (sNewProDetailID != null)
                {
                    sNewProDetailID = sNewProDetailID.Substring(0, sNewProDetailID.Length - 1);
                }
                else
                {
                    sNewProDetailID = "";
                }

                if (sNewProID != "" && sNewAmount != "")
                {
                    WriteCookies(sNewProID, sNewProDetailID, sNewAmount);
                }
                else
                {
                    this.Clear();
                }
            }
            //少了这一句的话 当修改完退出 浏览器时cookies 将会被清空
            HttpContext.Current.Response.Cookies["" + UIConfig.CookiesName + "ShoppingCart"].Expires = DateTime.Now.AddDays(60);
        }

        /// <summary>
        /// 统计购物车内商品数量
        /// </summary>
        /// <returns>商品数量</returns>
        public Int32 GetShopCarCount()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"];

            String c = String.Empty;
            if (cookie != null)
            {
                c = Convert.ToString(HttpContext.Current.Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["Amount"]);
            }
            string[] splitAmount = null;
            if (!String.IsNullOrEmpty(c) && CNVP.Framework.Utils.Public.IsNumericSet(c))
            {
                splitAmount=c.Trim().Split(new Char[] { ',' });
            }
            Int32 countProduct = 0;
            if (splitAmount!=null  && splitAmount.Length > 0)
            {
                foreach (String s in splitAmount)
                {
                    if (!String.IsNullOrEmpty(s) && CNVP.Framework.Utils.Public.IsNumber(s))
                    {
                        countProduct += Convert.ToInt32(s);
                    }
                }
            }
            return countProduct;
        }
    }
}
